package com.xrui.hbase.impl;

import com.google.common.base.Objects;
import com.xrui.hbase.ColumnName;
import com.xrui.hbase.schema.spec.ColumnReaderSpec;


/**
 * Binding of a {@link ColumnReaderSpec} to a specific column. The table in which the column can be
 * found is assumed based on the context in which the BoundColumnReaderSpec is used.
 */
public final class BoundColumnReaderSpec {
    private final ColumnReaderSpec mColumnReaderSpec;
    private final ColumnName mColumn;

    /**
     * Initialize a new BoundColumnReaderSpec. This binding does not
     * modify the ColumnReaderSpec, nor prevent it from being bound to other columns simultaneously.
     *
     * @param columnReaderSpec the ColumnReaderSpec to bind.
     * @param column           column to which to bind the ColumnReaderSpec.
     */
    private BoundColumnReaderSpec(
        final ColumnReaderSpec columnReaderSpec,
        final ColumnName column
    ) {
        mColumnReaderSpec = columnReaderSpec;
        mColumn = column;
    }

    /**
     * Bind a {@link ColumnReaderSpec} to the given column in the given table. This binding does not
     * modify the ColumnReaderSpec, nor prevent it from being bound to other columns simultaneously.
     *
     * @param columnReaderSpec the ColumnReaderSpec to bind.
     * @param column           column to which to bind the ColumnReaderSpec.
     * @return a new BoundColumnReaderSpec.
     */
    public static BoundColumnReaderSpec create(
        final ColumnReaderSpec columnReaderSpec,
        final ColumnName column
    ) {
        return new BoundColumnReaderSpec(columnReaderSpec, column);
    }

    /**
     * Get the ColumnReaderSpec which is bound to the given column.
     *
     * @return the ColumnReaderSpec which is bound to the given column.
     */
    public ColumnReaderSpec getColumnReaderSpec() {
        return mColumnReaderSpec;
    }

    /**
     * Get the column which the ColumnReaderSpec is bound.
     *
     * @return the column which the ColumnReaderSpec is bound.
     */
    public ColumnName getColumn() {
        return mColumn;
    }

    public ColumnName getColumnName() {
        return ColumnName.create(mColumn.getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return Objects.hashCode(mColumnReaderSpec, mColumn);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(
        final Object object
    ) {
        if (!(object instanceof BoundColumnReaderSpec)) {
            return false;
        }
        final BoundColumnReaderSpec that = (BoundColumnReaderSpec) object;
        return (Objects.equal(this.mColumnReaderSpec, that.mColumnReaderSpec)
            && (Objects.equal(this.mColumn, that.mColumn)));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return Objects.toStringHelper(BoundColumnReaderSpec.class)
            .add("column", mColumn)
            .add("column_reader_spec", mColumnReaderSpec)
            .toString();
    }
}
